package com.demo.finalClass;

import lombok.Data;

import java.util.*;

@Data
public class SmsRouter {

	private static volatile SmsRouter instance = new SmsRouter();

	private final Map<Integer, SmsInfo> smsInfoMap;

	public SmsRouter() {
		this.smsInfoMap = this.loadSmsInfoRouteFromDb();
	}

	private Map<Integer, SmsInfo> loadSmsInfoRouteFromDb() {
		Map<Integer, SmsInfo> routeMap = new HashMap<>();
		routeMap.put(1, new SmsInfo(1, "url1", 181L));
		routeMap.put(2, new SmsInfo(2, "url2", 182L));
		routeMap.put(3, new SmsInfo(3, "url3", 183L));
		return routeMap;
	}

	public Map<Integer, SmsInfo> getSmsRouteMap() {
		return Collections.unmodifiableMap(deepCopy(smsInfoMap));
	}

	public static void setInstance(SmsRouter newInstance) {
		instance = newInstance;
	}

	private Map<Integer, SmsInfo> deepCopy(Map<Integer, SmsInfo> smsInfoMap) {
		HashMap<Integer, SmsInfo> result = new HashMap<>(smsInfoMap.size());
		result.forEach((key, value) -> result.put(key, new SmsInfo(value)));
		return result;
	}

	public static void main(String[] args) {
		Map<String, List<String>> map = new HashMap<>();
		List<String> list = new ArrayList<>();
		list.add("value1");
		list.add("value2");

		map.put("key1", list);

		Map<String, List<String>> unmodifiableMap = Collections.unmodifiableMap(map);
		System.out.println(unmodifiableMap.get("key1").get(0));
		unmodifiableMap.get("key1").set(0, "value3");
		System.out.println(map.get("key1").get(0));
	}
}
